package com.skyz.wrap.dialog;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.FragmentManager;

import com.bumptech.glide.request.RequestOptions;
import com.skyz.base.dialog.BaseDialogFragment;
import com.skyz.base.util.ImageUtils;
import com.skyz.base.util.ScreenUtils;
import com.skyz.wrap.R;

public class TransferDialogFragment extends BaseDialogFragment {

    private static final String KEY_COVER = "COVER";
    private static final String KEY_NAME = "NAME";
    private static final String KEY_ID = "ID";

    public static void showDialogFragment(FragmentManager fragmentManager, String cover, String name,int id, OnActionClickListener onActionClickListener) {
        TransferDialogFragment indexDialogFragment = new TransferDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_COVER, cover);
        bundle.putString(KEY_NAME, name);
        bundle.putInt(KEY_ID, id);
        indexDialogFragment.setArguments(bundle);
        indexDialogFragment.setOnActionClickListener(onActionClickListener);
        indexDialogFragment.show(fragmentManager, null);
    }

    private String cover;
    private String name;
    private int id;

    private AppCompatImageView iv_cover;
    private AppCompatTextView tv_action;
    private AppCompatImageView iv_close;
    private AppCompatTextView tv_name;

    private OnActionClickListener onActionClickListener;

    public void setOnActionClickListener(OnActionClickListener onActionClickListener) {
        this.onActionClickListener = onActionClickListener;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_fragment_transfer;
    }

    @Override
    protected void onLayoutInflated(View view) {
        setDialogCanceledOnTouchOutside(false);

        iv_cover = view.findViewById(R.id.iv_cover);
        tv_action = view.findViewById(R.id.tv_action);
        iv_close = view.findViewById(R.id.iv_close);
        tv_name = view.findViewById(R.id.tv_name);


        tv_action.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                if (onActionClickListener != null) {
                    onActionClickListener.onActionClick();
                }
            }
        });
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                if (onActionClickListener != null) {
                    onActionClickListener.onCancel();
                }
            }
        });
    }

    @Override
    protected WindowManager.LayoutParams initLayoutParams(WindowManager.LayoutParams layoutParams) {
        layoutParams.width = (int) (ScreenUtils.getScreenWidth() - ScreenUtils.dp2px(80));
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.gravity = Gravity.CENTER;
        return layoutParams;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        getArgumentsData();
    }

    private void getArgumentsData() {
        Bundle arguments = getArguments();
        if (arguments == null) {
            return;
        }
        cover = arguments.getString(KEY_COVER);
        name = arguments.getString(KEY_NAME);
        id = arguments.getInt(KEY_ID);

        ImageUtils.showImage(iv_cover, cover, new RequestOptions());
        tv_name.setText(name);
    }

    public interface OnActionClickListener {
        void onActionClick();
        void onCancel();
    }
}
